package services;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import model.Constants;
import model.YouTubeEntry;

import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.youtube.PlaylistEntry;
import com.google.gdata.data.youtube.PlaylistLinkEntry;
import com.google.gdata.data.youtube.PlaylistLinkFeed;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.ServiceException;

public class PlaylistInteractionService {

	private YouTubeService service;
	
	public PlaylistInteractionService(YouTubeService youtubeservice)
	{
		this.service = youtubeservice;
	}
	
	
    public PlaylistLinkFeed getPlaylistsAsLinkFeed() throws IOException, ServiceException {
       
        PlaylistLinkFeed playlistLinkFeed = service.getFeed(new URL(Constants.playlistURL), PlaylistLinkFeed.class);
       
        return playlistLinkFeed;
     
    }
    
//    public void addToPlaylist(YouTubeEntry youTubeEntry){
//    	
//    	
//    	
//    	try {
//			service.insert(new URL(playlistLinkEntry.getFeedUrl()), playlistEntry);
//		} catch (MalformedURLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ServiceException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//    }
    
    
    public void addNewPlaylist(PlaylistLinkEntry playlistLinkEntry)
    {
        try {
           service.insert(new URL(Constants.playlistURL), playlistLinkEntry);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } catch (ServiceException ex) {
        	System.out.println(ex.getMessage());
        }
    }
    
	@Deprecated
    public void addToPlaylist(String playlist) throws ServiceException, IOException {
        VideoFeed videoFeed = null;
        String feedUrl = "http://gdata.youtube.com/feeds/api/users/default/uploads";
        try {
            videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
        } catch (MalformedURLException ex) {
        	System.out.println(ex.getMessage());
        }
        List<VideoEntry> entries = videoFeed.getEntries();
        VideoEntry update = entries.get(0);
        PlaylistEntry playlistEntry = new PlaylistEntry(update);
        service.insert(new URL(playlist), playlistEntry);
    }
	
	@Deprecated
    public void addPlaylist(String name, String description) {
        String feedUrl = "http://gdata.youtube.com/feeds/api/users/default/playlists";

        PlaylistLinkEntry newEntry = new PlaylistLinkEntry();
        newEntry.setTitle(new PlainTextConstruct(name));
        newEntry.setSummary(new PlainTextConstruct(description));
        try {
            PlaylistLinkEntry createdEntry = service.insert(new URL(feedUrl), newEntry);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } catch (ServiceException ex) {
        	System.out.println(ex.getMessage());
        }
    }
}
